﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formProductOutcome : FormMDIChildTemplate
    {
        public formProductOutcome()
        {
            InitializeComponent();
        }
        private void FillInvoice()
        {
            var invoiceOutcomes = from x in context.InvoiceOutcomes orderby x.DateCreate select x;
            invoiceOutcomeBindingSource.DataSource = invoiceOutcomes;
        }
        private void formMaterialProductOutputcome_Load(object sender, EventArgs e)
        {
            FillInvoice();
            root.TranslateGrid(gridInvoice);
            var storeHouse = context.StoreHouses.Select(x => x);
            storeHousebindingSource.DataSource = storeHouse;
        }

        private void gridInvoice_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            string value = e.Value as string;
            string columnName = gridInvoice.Columns[e.ColumnIndex].Name.ToUpper();
            if (columnName.Contains("STOREHOUSE"))
            {
                string s = e.Value as string;
                StoreHouse storeHouse = (from x in storeHousebindingSource.OfType<StoreHouse>() where x.ToString() == s select x).FirstOrDefault();
                e.Value = storeHouse;
                e.ParsingApplied = true;
            }


            e.ParsingApplied = true;
        }


        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            InvoiceOutcome invoiceOutcome = (InvoiceOutcome)invoiceOutcomeBindingSource.AddNew();
            invoiceOutcome.InvoiceOutcomeNumber = 1;
            if (gridInvoice.RowCount > 1)
                invoiceOutcome.InvoiceOutcomeNumber = context.InvoiceOutcomes.Max(x => x.InvoiceOutcomeNumber) + 1;
             
            invoiceOutcome.Vat = Convert.ToInt32(context.Rates.Where(x => x.RateID == 1).Select(x => x.RateValue).First());
            invoiceOutcome.IsPay = false;
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            buttonSelect.Focus();
            root.Submit(); FillInvoice();
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            /*
            reportInvoiceOutcome report = new reportInvoiceOutcome();

            var invoice = from invoiceOutcomeToHistories in context.InvoiceOutcomeToHistories
                          from histories in context.ProductHistories
                          where invoiceOutcomeToHistories.ProductHistory == histories && invoiceOutcomeToHistories.InvoiceOutcome ==
                          (InvoiceOutcome)invoiceOutcomeBindingSource.Current
                          select new ViewInvoice
                          {
                              Count = invoiceOutcomeToHistories.Count,
                              InvoiceToHistoryID = invoiceOutcomeToHistories.InvoiceOutcomeToHistoryID,
                              Price = invoiceOutcomeToHistories.Price,
                              PriceTotal = invoiceOutcomeToHistories.PriceTotal,
                              ProductName = histories.Product.ProductName,
                              ProductUnitName = histories.Product.Unit.UnitName
                          };
            report.DataSource = invoice;




            report.xrLabelFirmFrom.Lines = db.GetFirmOwn().GetFullDescription();

            InvoiceOutcome invoiceOutcome = (InvoiceOutcome)invoiceOutcomeBindingSource.Current;

            report.xrLabelFirmTo.Lines = invoiceOutcome.Object.Firm.GetFullDescription();
            report.xrLabelPayer.Text = invoiceOutcome.Payer;
            report.xrLabelHead1.Text = "Рахунок - фактура №  " + invoiceOutcome.InvoiceOutcomeNumber;
            report.xrLabelHead2.Text = "від " + invoiceOutcome.DateCreate.ToShortDateString();
            report.xrLabelPDV.Text = invoiceOutcome.Vat.ToString();
            decimal sum = 0;
            foreach (ViewInvoice viewInvoiceOutcome in invoice) sum += viewInvoiceOutcome.PriceTotal;
            decimal vAT = sum / 100 * invoiceOutcome.Vat;
            decimal totalSum = vAT + sum;

            report.xrLabelTotal.Text = context.sfMoneyToWords(totalSum);



            report.xrLabelSum.Text = decimal.Round(sum, 3).ToString();
            report.xrLabelSumTotal.Text = decimal.Round(totalSum, 3).ToString();
            report.xrLabelVAT.Text = decimal.Round(vAT, 3).ToString();

            report.ShowPreview();
             */ 

        }


        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            InvoiceOutcome invoiceOutcome = (InvoiceOutcome)invoiceOutcomeBindingSource.Current;
            if (invoiceOutcome != null)
            {
                db.DeleteInvoiceOutcome(invoiceOutcome);

                root.Submit();
                FillInvoice();
            }

        }

        private void gridInvoice_SelectionChanged(object sender, EventArgs e)
        {

        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (gridInvoice.Rows.Count > 0)
            {
                InvoiceOutcome invoiceOutcome = (InvoiceOutcome)invoiceOutcomeBindingSource.Current;
                root.ShowForm(new formProductOutcomeDetail(invoiceOutcome));
            }
        }

        private void buttonPrint2_Click(object sender, EventArgs e)
        {

        }

        private void buttonSelectFirm_Click(object sender, EventArgs e)
        {
            panelFirm.Visible = true;
            buttonSelectFirm.Visible = false;
            if (currentInvoiceOutcome != null) currentInvoiceOutcome.Object = null;

            firmBindingSource.DataSource = context.Firms.Where(x => x.FirmTypeID == 3);
            rateBindingSource.DataSource = context.Rates.Where(x => x.RateID >= 9 && x.RateID <= 11);
        }

        private void buttonSaveFirm_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                Object obj = (Object)objectBindingSource.Current;
                if (obj == null) return;
                currentInvoiceOutcome.Object = obj;
                root.Submit();
            }
            else
            {
                Firm firm = (Firm)firmBindingSource.Current;
                Rate rate = (Rate)rateBindingSource.Current;
                if (rate == null || firm == null) return;
                Object obj = new Object();
                obj.Firm = firm;
                obj.Rate = rate;
                obj.IsReal = false;
                obj.ObjectName = "Автогенерація " + DateTime.Now.ToString();
                context.Objects.InsertOnSubmit(obj);
                root.Submit();
                currentInvoiceOutcome.Object = obj;
                root.Submit();
            }
            HideFirmPanel();
        }

        private void HideFirmPanel()
        {
            panelFirm.Visible  = false;
            buttonSelectFirm.Visible = true;
        }


        InvoiceOutcome currentInvoiceOutcome;
        private void invoiceOutcomeBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            currentInvoiceOutcome = (InvoiceOutcome)invoiceOutcomeBindingSource.Current;
            if (currentInvoiceOutcome != null)
            {
                if (currentInvoiceOutcome.InvoiceOutcomeID == 0) buttonSelectFirm.Enabled = false;
                else buttonSelectFirm.Enabled = true;

                if (currentInvoiceOutcome.Object == null) buttonSelect.Enabled = false;
                else buttonSelect.Enabled = true;

                HideFirmPanel();
            }
        }

        private void firmBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            Firm firm = (Firm)firmBindingSource.Current;
            if (firm != null) objectBindingSource.DataSource = context.Objects.Where(x => x.Firm == firm && x.IsReal);
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxObject.Enabled = radioButton1.Checked;
            comboBoxRate.Enabled = radioButton2.Checked;
        }
    }
}
